package lib;

import java.net.InetAddress;
import java.net.Socket;
import java.io.Serializable;

public class ActorAddress implements Serializable, Cloneable{
	private InetAddress ip;
	private int port;
	static private int port_offset=20000;
	
	public ActorAddress(String host) throws Exception {
		this.ip=InetAddress.getByName(host);
		this.port=port_offset++;
	}
	public ActorAddress(String host, int port) throws Exception {
		this.ip=InetAddress.getByName(host);
		this.port=port;
	}
	public ListenSocket getListenSocket(String actorType,ActorAddress superActorAddress) {
		try{
			return new ListenSocket(port, actorType, superActorAddress.clone(), this.clone());
		}
		catch (Exception e){
			port=port_offset++;
			return getListenSocket(actorType, superActorAddress);
		}
	}
	public Socket getSocket() throws Exception{
		return new Socket(ip,port);
	}
	public boolean equals(ActorAddress actorAddr){
		if((this.ip.getHostName().equals(actorAddr.ip.getHostName()))&&(this.port==actorAddr.port)){
			return true;
		} else {
			return false;
		}
	}
	public String toString(){
		return this.ip.getHostAddress()+":"+String.valueOf(port);
	}
	public ActorAddress clone() {
		try {
			return new ActorAddress(ip.getHostName(),port);
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
